package com.fz.action;

import com.fz.dao.BookDAO;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import lombok.Data;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by webrx on 2017-09-15.
 */
@Repository
@Data
public class Hello extends ActionSupport{
    private String name;
    @Autowired
    private BookDAO dao;
    public void abc(){
        System.out.println("hello-abc");
        System.out.println(dao.query().size());
    }

    public String add(){
        return "success";
    }

    public String save(){
        dao.save(name);
        return "success";
    }

    public String index(){
        ActionContext.getContext().put("book",dao.query());
        return "index";
    }

    public String delete(){
        HttpServletRequest req = ServletActionContext.getRequest();
        int id = Integer.parseInt(req.getParameter("id"));
        dao.deleteById(id);
        return "success";
    }

    public void show() throws IOException {
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        for (int i = 0; i <= 9; i++){
            out.print(i + "、Hello....MVC框架测试<br/>");
        }
        System.out.println("helloworld...");
    }
}
